package com.spm.service.impl;

import com.spm.dao.GradeMapper;
import com.spm.pojo.Grade;
import com.spm.pojo.StudentGrade;
import com.spm.service.GradeService;
import com.spm.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class GradeServiceImpl implements GradeService {

    private GradeMapper gradeMapper;

    public GradeServiceImpl(SqlSession sqlSession) {
        sqlSession = MybatisUtils.getSqlSession();
        gradeMapper = sqlSession.getMapper(GradeMapper.class);
    }

    @Override
    public int uploadGrades(List<StudentGrade> studentGradeList) {
        for (StudentGrade studentGrade:
             studentGradeList) {
            //更改失败
            if(gradeMapper.updateGradeItem(studentGrade) ==0)
                return 0;
        }
        return 1;
    }

    @Override
    public int uploadOneGrade(StudentGrade studentGrade) {
        return gradeMapper.updateGradeItem(studentGrade);
    }

    @Override
    public List<Grade> queryForStudent(String sid) {
        return gradeMapper.getGradeById(sid);
    }

    @Override
    public List<StudentGrade> queryByCourse(String tid, String cno) {
        return gradeMapper.getGradeByCourse(tid, cno);
    }

    @Override
    public StudentGrade queryByCAS(String tid, String cno, String sid) {
        return gradeMapper.getGradeByCAS(tid, cno, sid);
    }

    @Override
    public void calculateScore(List<StudentGrade> studentGradeList) {
        for (StudentGrade studentGrade:
             studentGradeList) {
            gradeMapper.calculateGrade(studentGrade);
        }
    }

    @Override
    public List<StudentGrade> failScreen(String tid, String cno) {
        return gradeMapper.screenFail(tid, cno);
    }

    @Override
    public List<StudentGrade> scopeScreen(String tid,int low,int high) {
        return gradeMapper.scopeScreen(tid, low, high);
    }


}
